package cn.uniqueww;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.*;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author: uniqueww
 * @since: 2022-11-28 15:37
 **/
@SpringBootTest
@RunWith(SpringRunner.class)
@Slf4j
public class SpeingbootCeshi {


    @Resource
    private RedisTemplate redisTemplate;

    @Test
    public void testString(){
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set("phone","apple",10l, TimeUnit.SECONDS);

        Object phone = valueOperations.get("phone");
        log.info(phone.toString());
    }


    @Test
    public void testHash(){
        HashOperations hashOperations = redisTemplate.opsForHash();
        HashMap<String, String> map = new HashMap<>();
        map.put("name","xiaoming");
        map.put("sex","nan");
        map.put("age","18");
//        hashOperations.put("young","name","xiaoming");
//        hashOperations.put("young","sex","nan");
//        hashOperations.put("young","age","18");

        hashOperations.putAll("young",map);

        Set set = hashOperations.keys("young");
        for (Object o : set) {
            log.info(o.toString());
        }

        List young = hashOperations.values("young");
        for (Object o : young) {
           log.info(o.toString());
        }

        hashOperations.delete("young","age");
    }


    @Test
    public void testList(){
        ListOperations listOperations = redisTemplate.opsForList();
        listOperations.leftPushAll("animo","dog","mao","tuzi","laohu");


        List animo = listOperations.range("animo", 0, -1);
        for (Object o : animo) {
            log.info(o.toString());
        }
        Object animo1 = listOperations.rightPop("animo");
        log.info(animo1.toString());

    }


    @Test
    public void testSet(){
        SetOperations setOperations = redisTemplate.opsForSet();
        setOperations.add("colour","red","green","black","whit");

        setOperations.add("colour1","red","green","black","yellow");


        Set colour = setOperations.members("colour");
        for (Object o : colour) {
            log.info(o.toString());
        }
        //差集
        Set difference = setOperations.difference("colour", "colour1");
        for (Object o : difference) {
            log.info("diffrent->{}",o.toString());
        }
        //并集
        Set union = setOperations.union("colour", "colour1");
        for (Object o : union) {
            log.info("union->{}",o.toString());
        }
        //交集
        Set intersect = setOperations.intersect("colour", "colour1");
        for (Object o : intersect) {
            log.info("intersect->{}",o.toString());
        }

        setOperations.remove("colour", "red");
    }

    @Test
    public void testZset(){
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
        zSetOperations.add("fruit","apple",10l);
        zSetOperations.add("fruit","banana",11l);
        zSetOperations.add("fruit","pear",12l);
        zSetOperations.add("fruit","peach",13l);

        Set fruit = zSetOperations.range("fruit", 0, -1);
        for (Object o : fruit) {
            log.info(o.toString());
        }
        //在原来的基础上加权重
        Double aDouble = zSetOperations.incrementScore("fruit", "apple", 5);
        log.info("权重{}",aDouble);

        fruit = zSetOperations.range("fruit", 0, -1);
        for (Object o : fruit) {
            log.info("第二次—>{}",o.toString());
        }

        zSetOperations.remove("fruit", "apple");


    }

    @Test
    public void comment(){
        //获取所有的key
        Set keys = redisTemplate.keys("*");
        for (Object key : keys) {
            log.info("key为{}",key.toString());
        }

        //获取key对应value的类型
        DataType fruit = redisTemplate.type("fruit");
        log.info("fruit对应的类型为{}",fruit.name());

        //判断某个key是否存在
        Boolean hasKey = redisTemplate.hasKey("fruit");
        log.info(hasKey.toString());

        //删除某个key
        redisTemplate.delete("fruit");
    }

}
